package com.wanwu.channel.facade.impl.template;

import com.wanwu.channel.facade.dto.req.ChBaseReqDto;
import com.wanwu.channel.facade.dto.resp.ChBaseRespDto;

import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author runquanchen
 * @version 1.0
 * @date 2020-02-22 17:10
 * @project channel
 */
public class BaseFacade  {

    /**
     * @param templateName 自定义接口名称
     * @param reqDto 接口请求入参
     * @param checkParams 入参校验逻辑 支持lambda
     * @param deal 接口处理逻辑 支持lambda
     * @description: facade请求模版方法，封装了异常处理
     * @return:
     * @date: 2020-02-24 15:03
     *
     */

    public <T extends ChBaseReqDto, R extends ChBaseRespDto> R deal(String templateName, T reqDto, Class<R> rClass, Consumer<T> checkParams, Function<T,R> deal){
        return new FacadeTemplate<T, R>(templateName, reqDto,rClass) {
            @Override
            protected void checkParams(T req) {
                super.checkParams(reqDto);
                if (checkParams != null) {
                    checkParams.accept(reqDto);
                }
            }

            @Override
            protected R process() {
                if (deal != null) {
                    return deal.apply(reqDto);
                }
                return null;
            }
        }.execute();
    }
}
